package com.qf.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import java.util.Scanner;


public class QQClient {
	public static void main(String[] args) throws Exception{
		//客户端连接远程服务器
		Socket socket = new Socket("127.0.0.1",9999);
		
		
		//创建一个子线程接收服务器发送的消息
		 new Thread(new Reciver(socket)).start();;
		
		
		
		//客户端给服务器发送一条消息【输出  】
		
		//通过socket对象获取一个输出流
		OutputStream os= socket.getOutputStream();//字节输出流
		
		
		String message;
		
		Scanner input = new Scanner(System.in);
		while(true) {
			System.out.print("输入消息:");
			
			message=input.next();
			
			 byte[] bytes= message.getBytes("utf-8");
			
			 os.write(bytes);
		}
		
		
		
		
	}
}

class Reciver implements Runnable{
	private Socket socket;
	
	public Reciver(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			InputStream is = socket.getInputStream();
			
			byte[] buffer=new byte[1024];
			
			while(true) {
				int r=is.read(buffer);
				System.out.println(new String(buffer,0,r));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
